import { useK8sClusterRole } from "../store/role";

const clusterRoleStore = useK8sClusterRole();

export const checkPermissions = async p => {
  return true;
  const userClusterRoles = await clusterRoleStore.getRoles();
  const isAdmin = clusterRoleStore.isAdmin;
  if (isAdmin) {
    return true;
  }
  for (const clusterRole of userClusterRoles) {
    const scope = clusterRole.metadata.labels["kubepi.org/role-type"];
    if (clusterRole.rules.length > 0) {
      for (const rule of clusterRole.rules) {
        if (
          ((scope === p.scope || scope === "cluster") &&
            rule.apiGroups.includes("*")) ||
          rule.apiGroups.includes(p.apiGroup)
        ) {
          if (
            ((scope === p.scope || scope === "cluster") &&
              rule.resources.includes("*")) ||
            rule.resources.includes(p.resource)
          ) {
            if (
              ((scope === p.scope || scope === "cluster") &&
                rule.verbs.includes("*")) ||
              rule.verbs.includes(p.verb)
            ) {
              return true;
            }
          }
        }
      }
    }
  }
  return false;
};

export const hasNsPodsListPermission = async () => {
  return await checkPermissions({
    apiGroup: "",
    resource: "pods",
    verb: "list",
    scope: "namespace"
  });
};

export const hasNsPodsexecCreatePermission = async () => {
  return await checkPermissions({
    apiGroup: "",
    resource: "pods/exec",
    verb: "create",
    scope: "namespace"
  });
};

export const hasNsPodslogGetPermission = async () => {
  return await checkPermissions({
    apiGroup: "",
    resource: "pods/log",
    verb: "get",
    scope: "namespace"
  });
};

export const hasNsSvrsListPermission = async () => {
  return await checkPermissions({
    apiGroup: "",
    resource: "services",
    verb: "list",
    scope: "namespace"
  });
};
